package com.core.gig.web.action;

import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.core.as.ASException;
import com.core.gig.as.ASConfiguracao;
import com.core.gig.as.ASFactory;
import com.core.gig.dto.ClienteDTO;
import com.core.gig.util.GIGUtil;

/**
 * @author Lucas Ceni
 *
 */
public class ClienteAction
		extends GIGAction
{
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		return novo(mapping, form, request, response);
	}
	
	public ActionForward novo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		clearForm(form);
		return incluir(mapping, form, request, response);
	}

	public ActionForward incluir(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		setScreenType(request, SCR_INCLUDE);
		return mapping.findForward(FWD_DEFAULT);
	}
	
	public ActionForward salvar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		ClienteDTO dto = new ClienteDTO();
		GIGUtil.copyProperties(dto, form);
		
		try
		{
			ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
			as.salvarCliente(dto);
			
			if (id(form) == 0)
			{
				addMessage(request, "global.msg.sucess.inclusion", ICO_OK);
				dyna(form).set("id", "");
			}
			else
			{
				addMessage(request, "global.msg.sucess.alteration", ICO_OK);
				return novo(mapping, form, request, response);
			}
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		return incluir(mapping, form, request, response);
	}
	
	public ActionForward pesquisar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		ClienteDTO dto = new ClienteDTO();
		GIGUtil.copyProperties(dto, form);
		
		try
		{
			ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
			Collection<ClienteDTO> clientes = as.pesquisarClientes(dto);
			
			if (clientes.isEmpty())
				addMessage(request, "global.msg.alert.search", ICO_ALERT);
			
			request.setAttribute("clientes", clientes);
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		return mapping.findForward(FWD_LIST);
	}
	
	public ActionForward listar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		clearForm(form);
		try
		{
			ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
			request.setAttribute("clientes", as.listarClientes());
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		return mapping.findForward(FWD_LIST);
	}
	
	public ActionForward excluir(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		int id = id(form);
		
		ClienteDTO dto = new ClienteDTO();
		dto.setId(id);
		
		try
		{
			ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
			as.excluirCliente(dto);
			addMessage(request, "global.msg.sucess.exclusion", ICO_OK);
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
		}
		return listar(mapping, form, request, response);
	}
	
	public ActionForward alterar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		ClienteDTO dto = new ClienteDTO();
		dto.setId(id(form));
		
		try
		{
			ASConfiguracao as = ASFactory.getInstance().getASConfiguracao();
			
			dto = as.recuperarCliente(dto);
			GIGUtil.copyProperties(form, dto);
			dyna(form).set("desconto", GIGUtil.formatNumber(dto.getDesconto()));
			
			setScreenType(request, SCR_MODIFY);
		}
		catch (ASException e)
		{
			addMessage(request, e.getMessage(), ICO_ERROR);
			return listar(mapping, form, request, response);
		}
		return mapping.findForward(FWD_DEFAULT);
	}
}